﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tayninh2011
{
    public partial class FrmLandpriceOnRoad : RootForm
    {
        private static readonly FrmLandpriceOnRoad frmLandprice = new FrmLandpriceOnRoad();
        private DataSet dsRoadWithPrice = new DataSet();
        private DataSet dsRoad = new DataSet();
        private DataSet dsDinamic = new DataSet();
        private string dkDinamic = String.Empty;
        private ITnExToolsDataManager dataTool = new TnExToolsDataManager();
        

        private FrmLandpriceOnRoad()
        {
            InitializeComponent();
        }
        public static FrmLandpriceOnRoad CallMe
        {
            get { return frmLandprice; }
        }

        public new void Show()
        {
            if (isShown)
            {
                base.Show();
            }
            else
            {
                base.Show();
                
                isShown = true;
            }
            backgroundWorker1.RunWorkerAsync();
        }
        private static bool isShown = false;

        static FrmLandpriceOnRoad()
        {

            frmLandprice.FormClosing += new FormClosingEventHandler(frm_FormClosing);

        }
        private static void frm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            isShown = false;
            frmLandprice.Hide();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            string dk = "select * from "+ TnFeatureClassName.SDE_DUONG_GIADAT;
            //dsRoadWithPrice = dataTool.TnQueryBySQL(RootForm._arrUserInfo, dk);
            dk = "select * from " + TnFeatureClassName.SDE_DUONG;
            dsRoad = dataTool.TnQueryBySQL(RootForm._arrUserInfo, dk);
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                //this.dgvRoadWithPrice.DataSource = dsRoadWithPrice.Tables[0];
                this.dgvRoad.DataSource = dsRoad.Tables[0];
                dkDinamic = "select * from " + TnFeatureClassName.SDE_DUONG_GIADAT + " where maduong='" + dgvRoad.Rows[0].Cells["maduong"].Value.ToString() + "'";
                dsRoadWithPrice = dataTool.TnQueryBySQL(RootForm._arrUserInfo, dkDinamic);
                this.dgvRoadWithPrice.DataSource = dsRoadWithPrice.Tables[0];
                hideColsOfDgvRoadWithPrice();
                hideColsOfDgvRoad();
            }
            catch { }
        }

        private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
        {
            this.dgvRoadWithPrice.ContextMenu = new ContextMenu();
            MenuItem addNew = new MenuItem();
            addNew.Text = "&Thêm giá mới";
            this.dgvRoadWithPrice.ContextMenu.MenuItems.Add(addNew);
            if (dgvRoadWithPrice.SelectedRows.Count != 0 && e.Button == MouseButtons.Right)
            {
                this.dgvRoadWithPrice.ContextMenu.Show(dgvRoadWithPrice, new Point(e.X, e.Y));
            }
            addNew.Click += new EventHandler(addNew_Click);

        }

        void addNew_Click(object sender, EventArgs e)
        {
            //this.dataGridView1.Rows.Add(dataGridView1.SelectedRows[0].Index);
            //string fields = "OBJECTID_1,OBJECTID,maduong,tenduong,dorong,vatlieu,giadat,batdat,ketthuc,loaiduong,capduong,ghichu,Shape_Leng,Shape_Area,nam";
            //string vals="
            //string sql= "insert into "+ TnConst.FLD_DUONG_GIADAT+"("+fields+") values"+
            //dataTool.TnUpdateBySQL(RootForm._arrUserInfo,
        }

        private void dgvRoad_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int maduongIndex = dgvRoad.Columns["maduong"].Index;
            string maduong = dgvRoad.Rows[e.RowIndex].Cells[maduongIndex].Value.ToString();
            dkDinamic = "select * from " + TnFeatureClassName.SDE_DUONG_GIADAT+ " where maduong='" + maduong + "'";
            
            dsRoadWithPrice = dataTool.TnQueryBySQL(RootForm._arrUserInfo, dkDinamic);
            dgvRoadWithPrice.DataSource = dsRoadWithPrice.Tables[0];
            hideColsOfDgvRoadWithPrice();
            

            //dgvRoadWithPrice.DataSource = dsRoadWithPrice;
        }

        private void hideColsOfDgvRoad()
        {
            try
            {
                dgvRoad.Columns["OBJECTID"].Visible = false;
                dgvRoad.Columns["dorong"].Visible = false;
                dgvRoad.Columns["giadat"].Visible = false;
                dgvRoad.Columns["shape"].Visible = false;
            }
            catch { }

            
        }

        private void hideColsOfDgvRoadWithPrice()
        {
            try
            {
                dgvRoadWithPrice.Columns["OBJECTID"].Visible = false;
                //dgvRoadWithPrice.Columns["shape"].Visible = false;
                dgvRoadWithPrice.Columns["OBJECTID_1"].Visible = false;
            }
            catch { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
